package com.sxapp.controller;


import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sxapp.entity.User;
import com.sxapp.service.UserService;
import com.sxapp.util.BasedTurnToShorter;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	UserService userService;
	
	@RequestMapping("/addUser")
	public String get_addUser() {
		return "/userManage/addUser";//get page of addUser.html
	}
	
	@RequestMapping("/index")
	public String get_index() {
		return "index";//get page of index.html
	}
	
	@RequestMapping("/allUser")
	public void select_allUser() {
		
		List<User> userList = null;
		try {
			userList = userService.select_allUser();
			System.out.println(userList.size());
		} catch (Exception e) {
			
		}
		
		//return "index";//get page of index.html
	}
	
	@RequestMapping("/import_user")
	public String get_import_user() {
		return "admin/import_user";//get page of index.html
	}
	
	@RequestMapping("/import_user_action")
	@ResponseBody
	public int import_user_action(User user) {
		//pbkdf2:sha256:50000$3h4COj7g$88da273e403706f3030d78f13730ce633704836b67a7bbbb6a48d239ab01d4eb
		
		user.setId(BasedTurnToShorter.generateUUID(UUID.randomUUID()));
		user.set_password("pbkdf2:sha256:50000$3h4COj7g$88da273e403706f3030d78f13730ce633704836b67a7bbbb6a48d239ab01d4eb");
		
		if (user.getTag().equals("1") ) {
			user.setTag("STUDENT");
		}else if (user.getTag().equals("0") ) {
			user.setTag("TEACHER");
		}else {
			return 0;//fail: caused by info error 
		}
		try {
			userService.import_addUser(user);
		} catch (Exception e) {
			System.out.println(user.name);
			return 2;
		}
		return 1;//success
	}
}
